//
//  UIImage+extention.m
//  StockRight
//
//  Created by mac on 16/10/18.
//  Copyright © 2016年 SYGuQuan. All rights reserved.
//

#import "UIImage+extention.h"

@implementation UIImage (extention)

+(UIImage *)imageWithColor:(UIColor *)aColor{
    return [UIImage imageWithColor:aColor withFrame:CGRectMake(0, 0, 1, 1)];
}
+(UIImage *)imageWithColor:(UIColor *)aColor withFrame:(CGRect)aFrame{
    UIGraphicsBeginImageContext(aFrame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [aColor CGColor]);
    CGContextFillRect(context, aFrame);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

-(UIImage*)scaledToSize:(CGSize)targetSize highQuality:(BOOL)highQuality{
    if (highQuality) {
        targetSize = CGSizeMake(2*targetSize.width, 2*targetSize.height);
    }
    return [self scaledToSize:targetSize];
}

//对图片尺寸进行压缩--
-(UIImage*)scaledToSize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat scaleFactor = 0.0;
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetSize.width / imageSize.width;
        CGFloat heightFactor = targetSize.height / imageSize.height;
        if (widthFactor < heightFactor)
            scaleFactor = heightFactor; // scale to fit height
        else
            scaleFactor = widthFactor; // scale to fit width
    }
    scaleFactor = MIN(scaleFactor, 1.0);
    CGFloat targetWidth = imageSize.width* scaleFactor;
    CGFloat targetHeight = imageSize.height* scaleFactor;
    
    targetSize = CGSizeMake(floorf(targetWidth), floorf(targetHeight));
    UIGraphicsBeginImageContext(targetSize); // this will crop
    [sourceImage drawInRect:CGRectMake(0, 0, ceilf(targetWidth), ceilf(targetHeight))];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil){
        newImage = sourceImage;
    }
    UIGraphicsEndImageContext();
    return newImage;
}


+(UIImage*) imageWithColor:(UIColor*)color size:(CGSize)size {
    return [self imageWithColor:color size:size cornerRadius:0];
}

+(UIImage*) imageWithColor:(UIColor*)color size:(CGSize)size cornerRadius:(CGFloat)radius {
    CALayer* layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 0, size.width, size.height);
    layer.cornerRadius = radius;
    layer.backgroundColor = color.CGColor;
    
    //UIGraphicsBeginImageContext(size);
    float scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [layer renderInContext:ctx];
    
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

-(UIImage*) resizableImageWithConstrainedSize:(CGSize)maxSize {
    CGSize size = self.size;
    
    if(fabs(size.width - size.height) <= 1){
        size.width = MIN(size.width, size.height);
        size.height = MIN(size.width, size.height);
    }
    
    float width, height;
    
    if (size.width / size.height >= maxSize.width / maxSize.height) {
        
        width = MIN(size.width, maxSize.width);
        height = size.height * (width / size.width);
        
    } else {
        
        height = MIN(size.height, maxSize.height);
        width = size.width * (height / size.height);
        
    }
    
    if (width < size.width || height < size.height) {
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        [self drawInRect:CGRectMake(0, 0, width, height)];
        UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    
    return self;
}

@end
